package com.soft.mainsite.module.data.web;

import com.github.pagehelper.PageInfo;
import com.soft.mainsite.common.domain.RestResponse;
import com.soft.mainsite.module.data.dto.DataQueryParams;
import com.soft.mainsite.module.data.dto.FavoriteDTO;
import com.soft.mainsite.module.data.dto.UserDataDeleteDTO;
import com.soft.mainsite.module.data.entity.MainData;
import com.soft.mainsite.module.data.entity.UserData;
import com.soft.mainsite.module.data.service.DataService;
import com.soft.mainsite.module.data.service.UserDataService;
import com.soft.mainsite.module.data.viewmodel.CategoryVM;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author: hjm
 * @Date: 2020/11/28 23:19
 * Stay hungry,stay foolish!
 */
@Api(tags = "小程序使用相关接口", value = "userDataController")
@Controller
@RequestMapping(value = "/user")
public class UserDataController {

    @Resource
    private UserDataService userDataService;

    @Resource
    private DataService dataService;

    @ApiOperation(value = "小程序使用，领取资料")
    @PostMapping(value = "/save")
    @ResponseBody
    public RestResponse save(@RequestBody UserData userData) {

        RestResponse restResponse = userDataService.saveUserData(userData);

        return restResponse;
    }

    @ApiOperation(value = "小程序使用，添加收藏")
    @PostMapping(value = "/addFavorite")
    @ResponseBody
    public RestResponse addFavorite(@RequestBody FavoriteDTO favoriteDTO) {

        RestResponse restResponse = userDataService.addFavorite(favoriteDTO);

        return restResponse;
    }

    @ApiOperation(value = "小程序使用，取消收藏")
    @PostMapping(value = "/cancelFavorite")
    @ResponseBody
    public RestResponse cancelFavorite(@RequestBody FavoriteDTO favoriteDTO) {

        RestResponse restResponse = userDataService.cancelFavorite(favoriteDTO);

        return restResponse;
    }

    @ApiOperation(value = "小程序使用，条件查询学科列表和模糊搜索")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public RestResponse index(@RequestParam(value = "pageCurrent", defaultValue = "1") Integer pageNumber,
                              @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                              DataQueryParams queryParams) {

        PageInfo<MainData> pageInfo = dataService.pageDataByConditions(queryParams, pageNumber, pageSize);

        return RestResponse.success(pageInfo);
    }

    @ApiOperation(value = "小程序使用，体验页面六条数据")
    @RequestMapping(value = "/experience/list", method = RequestMethod.GET)
    @ResponseBody
    public RestResponse experienceIndex() {

        List<MainData> experienceList = dataService.experienceList();

        return RestResponse.success(experienceList);
    }

    @ApiOperation(value = "小程序使用，查询学科列表(根据学科名模糊查询学科列表)")
    @RequestMapping(value = "/categoryList", method = RequestMethod.GET)
    @ResponseBody
    public RestResponse categoryList(@RequestParam(value = "categoryName", required = false) String categoryName,
                                     @RequestParam("userPhone") String userPhone) {

        List<CategoryVM> categoryVmList = dataService.getCategoryList(categoryName, userPhone);

        return RestResponse.success(categoryVmList);
    }

    @ApiOperation(value = "小程序使用，根据用户手机号和资料id删除用户已领取的资料")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public RestResponse delete(@RequestBody UserDataDeleteDTO userDataDeleteDTO) {
        return userDataService.delete(userDataDeleteDTO);
    }

    @ApiOperation(value = "小程序使用，用户领取的资料列表，按时间")
    @PostMapping(value = "/userDataList")
    @ResponseBody
    public RestResponse userDataListByDate(@RequestBody FavoriteDTO favoriteDTO) {

        RestResponse restResponse = userDataService.userDataList(favoriteDTO);

        return RestResponse.success(restResponse);
    }

    @ApiOperation(value = "小程序使用，用户领取的资料列表，按省份")
    @PostMapping(value = "/userDataListByProvince")
    @ResponseBody
    public RestResponse userDataListByProvince(@RequestBody FavoriteDTO favoriteDTO) {

        RestResponse restResponse = userDataService.userDataList(favoriteDTO);

        return RestResponse.success(restResponse);
    }

    @ApiOperation(value = "小程序使用，查询学科列表(根据学科名模糊查询学科列表)")
    @RequestMapping(value = "/listByCategoryId", method = RequestMethod.GET)
    @ResponseBody
    public RestResponse userDataList(@RequestParam("categoryId") Integer categoryId,
                                     @RequestParam("userPhone") String userPhone) {

        List<MainData> mainDataList = dataService.userDataList(categoryId, userPhone);

        return RestResponse.success(mainDataList);
    }
}
